<?php
/**
 * 用户中心
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class member_securityControl extends BaseMemberControl {


    public function __construct() {
        parent::__construct() ;
		/**
		 * 读取语言包
		 */
		Language::read('member_home_member');
    }
	/**
	 * 安全中心
	 *
	 * @param 
	 * @return 
	 */
	public function indexOp() {
		/**
		 * 实例化模型
		 */
		$model_member	= Model('member');
		$member_info = $model_member->find($_SESSION['member_id']);
		if($member_info['member_privacy'] != ''){
			$member_info['member_privacy'] = unserialize($member_info['member_privacy']);
		}
		Tpl::output('member_info',$member_info);

		self::profile_menu('index','index');
		Tpl::showpage('member_security.index');
	}
	/**
	 * 我的资料【更多个人资料】
	 *
	 * @param 
	 * @return 
	 */
	public function authOp(){
		$type = $_GET['type'];
		switch ($type) {
			case '':
				header("Location: index.php?act=member_security&op=index"); 
				break;
			case 'modify_pwd':
				self::profile_menu('pwd','pwd');
				Tpl::showpage('member_security.passwd');
				break;
			case 'modify_email':
				self::profile_menu('email','email');
				Tpl::showpage('member_security.email');
				break;
			case 'modify_mobile':
				self::profile_menu('mobile','mobile');
				Tpl::showpage('member_security.mobile');
				break;
			case 'modify_paypwd':
				$model_member	= Model('member');
				$member_info = $model_member->find($_SESSION['member_id']);
				if ($member_info['member_email_bind']==0 && $member_info['member_mobile_bind']==0){
				showMessage('请先绑定邮箱或手机','index.php?act=member_security&op=index','html','error');
				}
				self::profile_menu('paypwd','paypwd');
				Tpl::showpage('member_security.paypwd');
				break;
		}
	}
	/**
	 * 密码修改
	 *
	 * @param 
	 * @return 
	 */
	public function modify_pwdOp() {
		$lang	= Language::getLangContent();
		/**
		 * 实例化模型
		 */
		$model_member	= Model('member');
		/**
		 * 填写密码信息验证
		 */
		$obj_validate = new Validate();
		$obj_validate->validateparam = array(
		array("input"=>$_POST["orig_password"],		"require"=>"true",		"message"=>$lang['home_member_old_password_null']),
		array("input"=>$_POST["new_password"],		"require"=>"true",		"message"=>$lang['home_member_new_password_null']),
		array("input"=>$_POST["confirm_password"],	"require"=>"true",		"validator"=>"Compare","operator"=>"==","to"=>$_POST["new_password"],"message"=>$lang['home_member_input_two_password_again']),
		);
		$error = $obj_validate->validate();
		if ($error != ''){
			showValidateError($error);
		}

		$member_info	= $model_member->where(array('member_name'=>trim($_SESSION['member_name']),'member_passwd'=>md5(trim($_POST['orig_password']))))->find();
		if(empty($member_info)) {
			showDialog($lang['home_member_old_password_wrong']);
		}
		$update	= $model_member->update(array('member_passwd'=>md5(trim($_POST['new_password'])),'member_id'=>$_SESSION['member_id']));
		if($update) {
			$message = $lang['nc_common_save_succ'];
		} else {
			$message = $lang['nc_common_save_fail'];
		}
		showDialog($message,'index.php?act=member_security&op=index',$update ? 'succ' : 'error');
	}
	/**
	 * email修改
	 *
	 * @param 
	 * @return 
	 */
	public function send_bind_emailOp() {
		$lang	= Language::getLangContent();
		/**
		 * 实例化模型
		 */
		$model_member	= Model('member');
		/**
		 * 填写密码信息验证
		 */
		$obj_validate = new Validate();
		$obj_validate->validateparam = array(
		array("input"=>$_POST["orig_password"],		"require"=>"true",		"message"=>$lang['home_member_password_null']),
		array("input"=>$_POST["email"],				"require"=>"true",		"validator"=>"email",	"message"=>$lang['home_member_input_email_again_format']),
		);
		$error = $obj_validate->validate();
		if ($error != ''){
			showValidateError($error);
		}
		/**
		 * 邮箱是否存在检查
		 */
		$chack_email	= $model_member->infoMember(array('member_email'=>trim($_POST['email']),'member_id'=>$_SESSION['member_id']));
		$email_array	= $model_member->infoMember(array('member_email'=>trim($_POST['email'])));
		if(empty($chack_email)) {
			if(!empty($email_array) && is_array($email_array)) {
				showDialog($lang['home_member_input_email_again_exists']);
			}
		}
		/**
		 * 密码是否正确检查
		 */
		$member_info	= $model_member->where(array('member_name'=>trim($_SESSION['member_name']),'member_passwd'=>md5(trim($_POST['orig_password']))))->find();
		if(empty($member_info)) {
			showDialog($lang['home_member_input_password_again_wrong']);
		}
		/**
		 * 修改电子邮件地址
		 */
		$update	= $model_member->update(array('member_email'=>trim($_POST['email']),'member_email_bind'=>'1','member_id'=>$_SESSION['member_id']));
		if($update) {
			$_SESSION['member_email'] = trim($_POST['email']);
			$message = $lang['nc_common_save_succ'];
		} else {
			$message = $lang['nc_common_save_fail'];
		}
		showDialog($message,'index.php?act=member_security&op=index',$update ? 'succ' : 'error');
	}

	/**
	 * 绑定手机
	 *
	 * @param 
	 * @return 
	 */
	public function modify_mobileOp() {
		$lang	= Language::getLangContent();
		/**
		 * 实例化模型
		 */
		$model_member	= Model('member');
		/**
		 * 填写密码信息验证
		 */
		$obj_validate = new Validate();
		$obj_validate->validateparam = array(
		array("input"=>$_POST["orig_password"],		"require"=>"true",		"message"=>$lang['home_member_password_null']),
		array("input"=>$_POST["mobile"],			"require"=>"true",		"validator"=>"mobile",	"message"=>''),
		);
		$error = $obj_validate->validate();
		if ($error != ''){
			showValidateError($error);
		}
		/**
		 * 手机号是否存在检查
		 */	
		 
		$chack_mobile	= $model_member->infoMember(array('member_mobile'=>trim($_POST['mobile']),'member_id'=>$_SESSION['member_id']));
		$mobile_array	= $model_member->infoMember(array('member_mobile'=>trim($_POST['mobile'])));
		if(empty($chack_mobile)) {
			if(!empty($mobile_array) && is_array($mobile_array)) {
				showDialog('该手机号已被其他账号绑定，请更换其他手机号！');
			}
		}
		/**
		 * 密码是否正确检查
		 */
		$member_info	= $model_member->where(array('member_name'=>trim($_SESSION['member_name']),'member_passwd'=>md5(trim($_POST['orig_password']))))->find();
		if(empty($member_info)) {
			showDialog($lang['home_member_input_password_again_wrong']);
		}
		/**
		 * 修改手机号
		 */
		$update	= $model_member->update(array('member_mobile'=>trim($_POST['mobile']),'member_mobile_bind'=>'1','member_id'=>$_SESSION['member_id']));
		if($update) {
			$_SESSION['member_mobile'] = trim($_POST['mobile']);
			$message = $lang['nc_common_save_succ'];
		} else {
			$message = $lang['nc_common_save_fail'];
		}
		showDialog($message,'index.php?act=member_security&op=index',$update ? 'succ' : 'error');
	}
	/**
	 * 设置支付密码
	 *
	 * @param 
	 * @return 
	 */
	public function modify_paypwdOp() {
		$lang	= Language::getLangContent();
		/**
		 * 实例化模型
		 */
		$model_member	= Model('member');
		/**
		 * 填写密码信息验证
		 */
		$obj_validate = new Validate();
		$obj_validate->validateparam = array(
		array("input"=>$_POST["orig_password"],		"require"=>"true",		"message"=>$lang['home_member_old_password_null']),
		array("input"=>$_POST["password"],		    "require"=>"true",		"message"=>$lang['home_member_new_password_null']),
		array("input"=>$_POST["confirm_password"],	"require"=>"true",		"validator"=>"Compare","operator"=>"==","to"=>$_POST["password"],"message"=>$lang['home_member_input_two_password_again']),
		);
		$error = $obj_validate->validate();
		if ($error != ''){
			showValidateError($error);
		}

		$member_info	= $model_member->where(array('member_name'=>trim($_SESSION['member_name']),'member_passwd'=>md5(trim($_POST['orig_password']))))->find();
		if(empty($member_info)) {
			showDialog($lang['home_member_old_password_wrong']);
		}
		$update	= $model_member->update(array('member_paypwd'=>md5(trim($_POST['password'])),'member_id'=>$_SESSION['member_id']));
		if($update) {
			$message = $lang['nc_common_save_succ'];
		} else {
			$message = $lang['nc_common_save_fail'];
		}
		showDialog($message,'index.php?act=member_security&op=index',$update ? 'succ' : 'error');
	}
	/**
	 * 用户中心右边，小导航
	 * 
	 * @param string	$menu_type	导航类型
	 * @param string 	$menu_key	当前导航的menu_key
	 * @return 
	 */
	private function profile_menu($menu_type,$menu_key='') {
		$menu_array		= array();
		switch ($menu_type) {
			case 'index':
				$menu_array	= array(
					array('menu_key'=>'index',	'menu_name'=>'账户安全','menu_url'=>'index.php?act=member_security&op=index'));
				break;
			case 'pwd':
				$menu_array	= array(
					array('menu_key'=>'index',	'menu_name'=>'账户安全','menu_url'=>'index.php?act=member_security&op=index'),
					array('menu_key'=>'pwd',	'menu_name'=>'修改登录密码','menu_url'=>'index.php?act=member_security&op=auth&type=modify_pwd'));
				break;
			case 'email':
				$menu_array	= array(
					array('menu_key'=>'index',	'menu_name'=>'账户安全','menu_url'=>'index.php?act=member_security&op=index'),
					array('menu_key'=>'email',	'menu_name'=>'邮箱绑定','menu_url'=>'index.php?act=member_security&op=auth&type=modify_email'));
				break;
			case 'mobile':
				$menu_array	= array(
					array('menu_key'=>'index',	'menu_name'=>'账户安全','menu_url'=>'index.php?act=member_security&op=index'),
					array('menu_key'=>'mobile',	'menu_name'=>'手机绑定','menu_url'=>'index.php?act=member_security&op=auth&type=modify_mobile'));
				break;
			case 'paypwd':
				$menu_array	= array(
					array('menu_key'=>'index',	'menu_name'=>'账户安全','menu_url'=>'index.php?act=member_security&op=index'),
					array('menu_key'=>'paypwd',	'menu_name'=>'设置支付密码','menu_url'=>'index.php?act=member_security&op=auth&type=modify_paypwd'));
				break;
		}
		Tpl::output('member_menu',$menu_array);
		Tpl::output('menu_key',$menu_key);
	}
}
